import os

class ArffError(Exception): pass
class FileNotExistError(Exception):pass

def initarff(f,featurecount,classnumber):
    print "f:"+str(f)+" fc:"+str(featurecount)+" cn:"+str(classnumber)
    print>>f,"@relation classify\n"
    for i in range(featurecount):
        print>>f,"@attribute f"+str(i)+" real"
        #classstr = classstr+str(i)
    classstr = "@attribute class {"
    for i in range(classnumber):
        classstr = classstr + str(i)+","
    classstr = classstr[0:len(classstr)-1]+"}"
    print>>f,classstr
    print>>f,""
    print>>f,"@data"

def initarfffeatures(f,features):
    """
    @param f the file to output arff file
    @param features the type of the features
    """
    print>>f,"@relation classify\n"
    for i in range(len(features)):
        print>>f,"@attribute f"+str(i)+" "+features[i]
    print>>f,""
    print>>f,"@data"

def initArffFeaturesDic(f,features,featuredic):
    """
    @param f file object to write file
    @param features the feature in the right order
    @param featuredic from feature to the type of feature
    """
    print>>f,"@relation classify\n"
    for i in range(len(features)):
        featurestr = str(features[i])
        featurestr = featurestr.replace(" ","_")
        print>>f,"@attribute f"+featurestr+" "+str(featuredic[features[i]])
    print>>f,""
    print>>f,"@data"

def initarffboost(f,featurecount,classnumber):
    """
    feature is nominal
    """
    print "f:"+str(f)+" fc:"+str(featurecount)+" cn:"+str(classnumber)
    print>>f,"@relation classify\n"

    for i in range(featurecount):
        s = "@attribute f"+str(i)+" {"
        for j in range(classnumber):
            s = s + str(j) +','
        s = s[0:len(s)-1]+"}"
        print>>f,s
        #classstr = classstr+str(i)
        
    classstr = "@attribute class {"
    for i in range(classnumber):
        classstr = classstr + str(i)+","
    classstr = classstr[0:len(classstr)-1]+"}"
    
    print>>f,classstr
    print>>f,""
    print>>f,"@data"

def parserArffItems(fname):
    """
    relize as a class may be better choice
    @return the header part
    @return the data of each line
    """
    if os.path.isfile(fname) == False:
        raise FileNotExistError("FileNotExist : "+fname)
    
    f = open(fname)
    lines = f.readlines()
    f.close()
    
    headstr = ""
    k = 0
    for i in range(len(lines)):
        #print lines[i]
        headstr = headstr + lines[i]
        if lines[i].startswith("@data"):
            k = i+1
            break

    datas = []
    for j in range(k,len(lines)):
        if lines[j] == "\n" or lines[j] == "":
            continue
        s = lines[j]
        if s.endswith("\n"):
            s = s[0:len(s)-1]
        datas.append(s)
        
    dic = {}
    dic['head'] = headstr
    dic['datas'] = datas
    
    return dic
    
def getAttrList(fname):
    """
    get the list of attribute
    the name and type of each attribute
    """
    if os.path.isfile(fname) == False:
        raise FileNotExistError("FileNotExist:"+fname)

    f = open(fname)
    lines = f.readlines()
    f.close()

    result = []
    for line in lines:
        if line.endswith("\n"):
            line = line[0:len(line)-1]
        if line.startswith("@attri"):
            blank1 = line.index(" ")
            blank2 = line.index(" ",blank1+1)
            name = line[blank1+1:blank2]
            t = line[blank2+1:len(line)]
            dic = {}
            dic['name'] = name
            dic['type'] = t
            result.append(dic)
            
    return result
            
    
            
    
    
